# -*- coding: utf-8 -*-

"""
controller for fuwa chat
"""

from framework.admincontroller import AdminController
from models.fuwa import FuwaModel
import tornado.web
import time
import json

class FuwaChatCtrl(AdminController):
    """action index method"""

    @tornado.web.authenticated
    def get(self, *args):
        """fuwa chat"""
        if not args:
            self.render("fuwa/fuwachat.html")
            return
        msg = dict(err=0, msg=u'')
        m = FuwaModel()
        fuwa_list = m.get_last_count()
        fuwa = list()
        if not fuwa_list:
            self.write_error(500)
            return
        # api json
        if args[0] == 'fuwa':
            for i in fuwa_list:
                fuwa.append({
                    'day': time.strftime("%m-%d", time.localtime(i["sync_time"])),
                    'value': i["fu_count"]
                })
            msg['msg'] = fuwa
        elif args[0] == 'fuwa-i':
            for i in fuwa_list:
                fuwa.append({
                    "day": time.strftime("%m-%d", time.localtime(i["sync_time"])),
                    "value": i["fu_i_count"]
                })
            msg["msg"] = fuwa
        elif args[0] == 'fuwa-c':
            for i in fuwa_list:
                fuwa.append({
                    "day": time.strftime("%m-%d", time.localtime(i["sync_time"])),
                    "value": i["fu_c_count"]
                })
            msg["msg"] = fuwa
        elif args[0] == 'fuwa-i-hide':
            for i in fuwa_list:
                fuwa.append({
                    "day": time.strftime("%m-%d", time.localtime(i["sync_time"])),
                    "value": i["fu_i_hide"]
                })
            msg["msg"] = fuwa
        elif args[0] == 'fuwa-c-hide':
            for i in fuwa_list:
                fuwa.append({
                    "day": time.strftime("%m-%d", time.localtime(i["sync_time"])),
                    "value": i["fu_c_hide"]
                })
            msg["msg"] = fuwa
        elif args[0] == 'fuwa-i-caught':
            for i in fuwa_list:
                fuwa.append({
                    "day": time.strftime("%m-%d", time.localtime(i["sync_time"])),
                    "value": i["fu_i_caught"]
                })
            msg["msg"] = fuwa
        elif args[0] == 'fuwa-c-caught':
            for i in fuwa_list:
                fuwa.append({
                    "day": time.strftime("%m-%d", time.localtime(i["sync_time"])),
                    "value": i["fu_c_caught"]
                })
            msg["msg"] = fuwa
        else:
            msg["err"] = 404
            msg["msg"] = u"找不到访问"
            msg["href"] = "/"
        self.ajax_out(**msg)
        return
